package cn.zfs.datasource.util;

import java.util.ArrayList;
import java.util.List;

public class StringUtil {

    public static String formatString(String str, String replaceStr, String regex, Object... objects) {
        if (replaceStr == null || regex == null) {
            return str;
        }
        if (objects.length > 0 && str.contains(replaceStr)) {
            for (Object obj : objects) {
                str = str.replaceFirst(regex, obj == null ? "null" : String.valueOf(obj));
            }
        }
        return str;
    }

    public static String list2StringAndAppendStr(List<String> strings, String oneStr, String inStr, String endStr) {
        StringBuffer stringBuffer = new StringBuffer();
        boolean isNotOne = false;
        for (String string : strings) {
            if (isNotOne == false) {
                stringBuffer.append(oneStr);
            }
            if (isNotOne) {
                stringBuffer.append(inStr);
                stringBuffer.append(string);
            } else {
                stringBuffer.append(string);
                isNotOne = true;
            }
        }
        stringBuffer.append(endStr);
        return stringBuffer.toString();
    }

}
